<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="投票名称" prop="voteName">
        <el-input v-model="queryParams.voteName" placeholder="请输入投票名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['vote:info:create']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['vote:info:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="投票名称" align="center" prop="voteName" />
      <el-table-column label="规则类型" align="center" prop="voteType" >
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.VOTE_RULE_TYPE" :value="scope.row.voteType"/>
        </template>
      </el-table-column>

      <el-table-column label="投票开始时间" align="center" prop="beginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投票结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
         <template slot-scope="scope">
           <el-tag type="info" v-if="scope.row.endTime<new Date()">已结束</el-tag>
           <el-tag type="success" v-else-if="scope.row.beginTime>new Date()">未开始</el-tag>
           <el-tag type="danger" v-else-if="scope.row.beginTime<new Date() && scope.row.endTime>new Date()">进行中</el-tag>
         </template>
      </el-table-column>
      <el-table-column label="排序号" align="center" prop="weight" />
      <el-table-column label="实际投票人数" align="center" prop="voteCount" />
      <!-- <el-table-column label="计划投票人数" align="center" prop="limitCount" /> -->
      <el-table-column label="被投票人数" align="center" prop="importCount" /> 
      <el-table-column label="党政主要领导二维码" align="center">
         <template slot-scope="scope">
            <el-button type="text" @click="getVoteQrCode(scope.row,1)">查看</el-button>
         </template>
      </el-table-column>
      <el-table-column label="其他领导二维码" align="center">
         <template slot-scope="scope">
            <el-button type="text" @click="getVoteQrCode(scope.row,2)">查看</el-button>
         </template>
      </el-table-column>
      <el-table-column label="课题组组长二维码" align="center">
         <template slot-scope="scope">
            <el-button type="text" @click="getVoteQrCode(scope.row,4)">查看</el-button>
         </template>
      </el-table-column>
      <el-table-column label="课题组成员二维码" align="center">
         <template slot-scope="scope">
            <el-button type="text" @click="getVoteQrCode(scope.row,3)">查看</el-button>
         </template>
      </el-table-column>
      <el-table-column label="是否发布" align="center">
        <template slot-scope="scope">
          <el-switch
            @change="pushState(scope.row,$event)"
            v-model="scope.row.isPush"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            >
          </el-switch>
        </template>
        
      </el-table-column>
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
       <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['vote:info:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCopy(scope.row)"
                     v-hasPermi="['vote:info:update']">复制</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['vote:info:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="投票名称" prop="voteName">
          <el-input v-model="form.voteName" placeholder="请输入投票名称" />
        </el-form-item>
        <el-form-item label="投票开始时间" prop="beginTime">
          <el-date-picker clearable v-model="form.beginTime" type="datetime" value-format="timestamp" placeholder="选择投票开始时间" />
        </el-form-item>
        <el-form-item label="投票结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="datetime" value-format="timestamp" placeholder="选择投票结束时间" />
        </el-form-item>
        <!-- <el-form-item label="规则类型" prop="voteType">
           <el-select v-model="form.voteType" :disabled="disabledVoteType" placeholder="规则类型(添加后不可修改)" clearable style="width: 240px">
              <el-option v-for="dict in voteTypeDictDatas" :key="parseInt(dict.value)" :label="dict.label" :value="parseInt(dict.value)"/>
           </el-select>
        </el-form-item> -->
        <el-form-item label="计划投票人数" prop="limitCount">
          <el-input-number :min="1" v-model="form.limitCount" placeholder="计划投票人数" />
        </el-form-item>
        <el-form-item label="权重" prop="weight">
          <el-input-number :min="1" v-model="form.weight" placeholder="越大越靠前" />
        </el-form-item>
        
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="qrCodetitle" :visible.sync="qrcodeOpenImg" width="600px" append-to-body>
       <div class="demo-image__placeholder">
        <el-image :src="voteQrcodeImg"></el-image>
       </div>
       <!-- <div slot="footer" class="dialog-footer">
        <el-button @click="cancelQrcodeImg">关 闭</el-button>
      </div> -->
    </el-dialog>

    <el-dialog :title="copyVoteInfo" :visible.sync="isCopy" width="800px" append-to-body>
      <el-form label-width="150px">
        <el-form-item label="投票名称">
          <el-input v-model="copyForm.voteName" placeholder="请输入投票名称" />
        </el-form-item>
        <!-- <el-form-item label="是否复制专家">
            <el-radio v-model="copyForm.copyVoter" :label="true">是</el-radio>
            <el-radio v-model="copyForm.copyVoter" :label="false">否</el-radio>
        </el-form-item> -->
        <el-form-item label="是否复制被投票人">
            <el-radio v-model="copyForm.copyBeVoter" :label="true">是</el-radio>
            <el-radio v-model="copyForm.copyBeVoter"  :label="false">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="copy">复 制</el-button>
        <el-button @click="isCopy = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createInfo, updateInfo, deleteInfo, getInfo, getInfoPage, exportInfoExcel,getQrCode,pushVote,copyVote} from "@/api/vote/info";
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
export default {
  name: "Info",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投票规则列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 是否复制
      copyVoteInfo: '复制投票信息',
      
      isCopy: false,
      copyForm:{
        copyVoter:false,
        copyBeVoter:false
      },
      // 是否禁用voteType
      disabledVoteType: false,
      dateRangeBeginTime: [],
      dateRangeEndTime: [],
      dateRangeCreateTime: [],
      // 二维码图片流
      voteQrcodeImg: "",
      qrcodeOpenImg: false,
      qrCodetitle: "",
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        voteName: null,
        voteCount: null,
        limitCount: null,
        noInVoteType: [1],
        remark: null,
        isPush: null,
        
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        voteName: [
          { required: true, message: "投票名称不能为空", trigger: "blur" }
        ],
        beginTime: [
          { required: true, message: "投票开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "投票结束时间不能为空", trigger: "blur" }
        ],
      },
      voteTypeDictDatas: getDictDatas(DICT_TYPE.VOTE_RULE_TYPE),
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeBeginTime, 'beginTime');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getInfoPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        voteName: undefined,
        beginTime: undefined,
        endTime: undefined,
        voteCount: undefined,
        limitCount: undefined,
        remark: undefined,
        importCount: undefined,
        voteType: 2,
        weight: 10,
        isPush: undefined,
      };
      this.disabledVoteType =false;
      this.resetForm("form");
    },

    pushState(row,state){
      row.isPush = state;
      let data = {id:row.id,isPush:state}
      pushVote(data).then(resp=>{
        this.$modal.msgSuccess("操作成功");
      })
    },

    handleCopy(row){
      this.copyForm={copyVoter:false,copyBeVoter:false};
      this.copyForm.id = row.id;
      this.isCopy=true;
      this.copyVoteInfo='复制"'+row.voteName+'"的投票规则';
    },

    copy(){
      if(this.copyForm.voteName=='' || this.copyForm.voteName==null){
        this.$modal.msgError("投票名称不能为空");
        return;
      }
      copyVote(this.copyForm).then(resp=>{
        this.$message({
          type: 'success',
          message: '复制成功!'
        });
        this.isCopy=false;
        this.getList();
      }).catch(() => {
        this.loading = false;
      });
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeBeginTime = [];
      this.dateRangeEndTime = [];
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投票规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.disabledVoteType=true;
      const id = row.id;
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改投票规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateInfo(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createInfo(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除投票规则编号为"' + id + '"的数据项?').then(function() {
          return deleteInfo(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeBeginTime, 'beginTime');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有投票规则数据项?').then(() => {
          this.exportLoading = true;
          return exportInfoExcel(params);
        }).then(response => {
          this.$download.excel(response, '投票规则.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
     getVoteQrCode(row,votePersonType){
      this.qrcodeOpenImg = true;
      if(votePersonType==1){
        this.qrCodetitle="党政主要领导二维码"
      }else if(votePersonType==2){
        this.qrCodetitle="其他领导二维码"
      }else if(votePersonType==3){
        this.qrCodetitle="课题组成员二维码"
      }else if(votePersonType==4){
        this.qrCodetitle="课题组组长二维码"
      }


      let query = {};
      query.votePersonType = votePersonType;
      query.voteId = row.id;
      getQrCode(query)
      .then(response=>{
        // console.log(response)
        this.voteQrcodeImg = "data:image/png;base64,"+ response.data;
      })
    },
    cancelQrcodeImg(){
      this.qrcodeOpenImg = false;
    }
  }
};
</script>
